use std::sync::atomic::AtomicU64;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::atomic::AtomicUsize;
use std::fs::{self, File};
use base64;
use std::io::{self, Read};
use serde_json::json;
use crate::filedirfunc;
use std::path::Path;
use std::sync::Arc;
use tokio;
use crate::gpuencode;
use std::time::{SystemTime, UNIX_EPOCH};
use winapi::um::sysinfoapi::MEMORYSTATUSEX;
use std::thread;
use tokio::future;
use std::mem::size_of;
use crate::nodelist;
use std::sync::Mutex;
use lazy_static::lazy_static;
use std::sync::Condvar;
use crate::ThreadSafeVariable::ThreadSafeVariable;

lazy_static! {
static ref netclientquene:nodelist::CrsQueue<reqwest::blocking::Client>= nodelist::CrsQueue::<reqwest::blocking::Client>::new();
static  ref memorysize:ThreadSafeVariable=ThreadSafeVariable::new(1024*1024*1024);//1GB
static  ref disksize:ThreadSafeVariable=ThreadSafeVariable::new(1024*1024*2);//2mb
static  ref netsize:ThreadSafeVariable=ThreadSafeVariable::new(1024*100);//100kb
}
pub fn fileupload(path:String,owner:String,repo:String,token:String,fullpath:String){
    let path = path.replace(r"\","/");
    let message = "Upload ".to_owned()+&path; // 提交消息
    let filelen = fs::metadata(fullpath.clone()).unwrap().len() as u64;
    memorysize.add(filelen);
    disksize.add(filelen);
    let file_content = fs::read_to_string(fullpath);
    disksize.sub(filelen);
    let mut base64_content:String;
    match file_content{
        Ok(res)=>{
            base64_content = gpuencode::base64_cuda(res);
            //base64_content = base64::encode(&res);
        }
        Err(err)=>{
            memorysize.sub(filelen);
            return;
        }
    }
    // 构建 API 请求
    let url = format!(
        "https://gitee.com/api/v5/repos/{}/{}/contents/{}",
        &owner, &repo, &path
    );
    let data = json!({
        "access_token": token,
                "owner":owner,
                "path":path,
                "content": base64_content,
                "message": message,
    });
    //gc
    drop(path);
    drop(message);
    let datalen = base64_content.len() as u64;
    drop(base64_content);
    //let client = reqwest::blocking::Client::new();
    let mut client:reqwest::blocking::Client;
    loop{
        let clientres = netclientquene.pop();
        match clientres{
            Some(res)=>{client=res;}
            None=>{client=reqwest::blocking::Client::new();}
        }
        netsize.add(datalen);
        let response = client
            .post(&url)
            .json(&data)
            .send();
        netsize.sub(datalen);
        netclientquene.push(client);
        match response{
            Ok(res)=>{
                if res.status() == 400{//400 retry
                    continue;
                }
                break;
            }
            Err(err)=>{
                continue;
            }
        }
    }
    memorysize.sub(filelen);
    return;
}